// 封装分类数据业务相关代码
import { ref, onMounted } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { getCategoryAPI } from '@/apis/category'


export function useCategory() {
   const categoryData = ref({})
   const route = useRoute()
   const getCategoryData = async (id = route.params.id) => {
      const res = await getCategoryAPI(id)
      categoryData.value = res.result
   }
   onMounted(() => getCategoryData())

   // 目标：路由参数变化的时候，可以把分类数据接口重新发送
   onBeforeRouteUpdate((to) => {
      // 存在问题，路由参数滞后性，拿不到最新路由参数，借助to参数
      getCategoryData(to.params.id)
   })
   return {
      categoryData
   }
}